#include "LogicList.h"

string LogicList::listAllTasks(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {
	
	string statusMessage;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	dataFeedback = _data->getAll(tasks);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskBySearchTerm(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {
	
	string statusMessage;
	string searchTerm;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	searchTerm = parsedCommand.listSearchTerm;
	
	dataFeedback = _data->getBySearchTerm(tasks, searchTerm);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskByDay(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {

	string statusMessage;
	ptime time;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	//
	time = parsedCommand.addStartTime;

	dataFeedback = _data->getByTime(tasks, time, time);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskByDate(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {
	
	string statusMessage;
	ptime time;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	//
	time = parsedCommand.addStartTime;

	dataFeedback = _data->getByTime(tasks, time, time);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskNextWeek(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {

	string statusMessage;
	ptime week;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	// ptime t = parsedCommand.startTime + dd(7);
	// temporary ptime week;
	
	dataFeedback = _data->getByTime(tasks, week, week);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskByMonth(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {

	string statusMessage;
	ptime month;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	month = parsedCommand.addStartTime;

	dataFeedback = _data->getByTime(tasks, month, month);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

string LogicList::listTaskByDone(Parser::PARSED_COMMAND parsedCommand, 
									vector<Task>& tasksVector,
									Data* _data,
									Writer* _writer) {

	string statusMessage;
	Data::DataFeedback dataFeedback;
	vector<Task> tasks;

	dataFeedback = _data->getByDone(tasks);

	if( dataFeedback == Data::NOT_FOUND ) {
		throw(NOT_FOUND_ERROR_MESSAGE);
	}

	tasksVector.clear();
	copyTasks(tasks, tasksVector);

	if( dataFeedback == Data::SUCCESS ) {
		statusMessage = LIST_SUCCESS_MESSAGE;
	} else {
		statusMessage = LIST_ERROR_MESSAGE;
		//logErrors(statusMessage, _undoState);
	}

	return statusMessage;
}

void LogicList::copyTasks(vector<Task>& tasks, vector<Task>& tasksVector) {
	for( unsigned int i=0; i<tasks.size(); i++ ) {
		tasksVector.push_back(tasks[i]);
	}
}

const string LogicList::LIST_SUCCESS_MESSAGE = "List Completed";

const string LogicList::LIST_ERROR_MESSAGE = "Unable To List";

const string LogicList::NOT_FOUND_ERROR_MESSAGE = "Task Not Found";